package com.example.time_planner.activities;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.example.time_planner.R;
import com.example.time_planner.drawer_menu.NavigationDrawerAdapter;
import com.example.time_planner.drawer_menu.NavigationItem;
import com.example.time_planner.fragments.NextTaskFragment;
import com.example.time_planner.fragments.PieChartFragment;

public class MainActivity extends FragmentActivity {

	private DrawerLayout drawLayout;
	private ActionBarDrawerToggle drawerToggle;
	private ListView leftDrawerList;
	private FrameLayout nextTaskContainer;
	private FrameLayout pieChartContainer;
	private List<NavigationItem> objects = new ArrayList<NavigationItem>();

	final private android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
	final private android.support.v4.app.FragmentTransaction transaction = fm
			.beginTransaction();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		getActionBar().setTitle(R.string.menu);
		getActionBar().setIcon(R.drawable.ic_drawer);

		settingTheFragmentsInFrameLayout();
		setTheDrawableMenu();

	}

	private void startActivity(Context context, Class<?> cls) {
		Intent activity = new Intent(context, cls);
		startActivity(activity);
	}

	private void settingTheFragmentsInFrameLayout() {
		nextTaskContainer = (FrameLayout) findViewById(R.id.next_task_main_screen_fragment_container);
		pieChartContainer = (FrameLayout) findViewById(R.id.pie_chart_fragment_main_screen_container);

		final NextTaskFragment nextTaskFr = new NextTaskFragment();
		final PieChartFragment pie = new PieChartFragment();

		transaction.add(R.id.next_task_main_screen_fragment_container,
				nextTaskFr);
		transaction.add(R.id.pie_chart_fragment_main_screen_container, pie);
		transaction.commit();

	}

	private void setTheDrawableMenu() {

		drawLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

		drawLayout.setDrawerListener(drawerToggle);

		getActionBar().setDisplayHomeAsUpEnabled(false);
		getActionBar().setHomeButtonEnabled(true);

		drawerToggle = new ActionBarDrawerToggle(this, drawLayout,
				R.drawable.ic_drawer, R.string.drawer_description,
				R.string.drawer_description_closed) {

			@Override
			public void onDrawerClosed(View drawerView) {
				super.onDrawerClosed(drawerView);
				getActionBar().setTitle(R.string.menu);
				getActionBar().setIcon(R.drawable.ic_drawer);
			}

			@Override
			public void onDrawerOpened(View drawerView) {
				super.onDrawerOpened(drawerView);
				getActionBar().setTitle(R.string.app_name);
				getActionBar().setIcon(R.drawable.ic_launcher);
			}
		};

		leftDrawerList = (ListView) findViewById(R.id.left_drawer);

		NavigationDrawerAdapter adapter = new NavigationDrawerAdapter(this,
				R.layout.navigation_drawer_simple_item, objects);
		leftDrawerList.setAdapter(adapter);

		leftDrawerList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				switch (position) {
				case 0:
					startActivity(MainActivity.this, CreateTaskActivity.class);
					break;
				case 1:
					startActivity(MainActivity.this,
							TimeManagementStatistics.class);
					break;
				default:
					break;
				case 2:
					startActivity(MainActivity.this, TaskList.class);
					break;
				}
			}
		});
		initItems();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		if (drawerToggle.onOptionsItemSelected(item)) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private void initItems() {

		objects.add(new NavigationItem(R.drawable.ic_action_new_event,
				"Create task"));
		objects.add(new NavigationItem(R.drawable.ic_action_about, "Statistics"));
		objects.add(new NavigationItem(R.drawable.ic_action_view_as_list,
				"Tasks"));

	}
}
